package saxion.sensorframework.service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Properties;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.apache.log4j.Logger;
import saxion.sensorframework.ClientProcessor;

/**
 * Webservice for client
 * 
 * @author Erik & Martijn
 * 
 */
@WebService
public class ClientWebService {

	static Logger logger = Logger.getLogger(SensorWebService.class.getName());
	/** < logger \sa saxion.sensorframework.SensorFramework::logger */
	private Properties properties;
	/**
	 * < Properties file. \sa saxion.sensorframework.SensorFramework::properties
	 */
	private ClientProcessor clientProcessor;

	/** < used to help with client related tasks */

	/**
	 * \brief ClientWebservice
	 * @param properties
	 */
	public ClientWebService(Properties properties) {
		this.properties = properties;
		initialize();
	}

	/**
	 * \brief initialize variables.
	 * 
	 * Create ClientProcessor.
	 * 
	 * \sa clientProcessor
	 * 
	 */
	private void initialize() {

		try {
			clientProcessor = new ClientProcessor(properties);
		} catch (Exception e) {
			logger.error("Unable to create ClientProcessor");
			e.printStackTrace();
			System.exit(-1);
		}
	}

	/**
	 * \brief Get sensors from database
	 * 
	 * Get al the sensor information form the database using the \link
	 * clientProcessor \endlink . The information is writen to xml document and
	 * returned
	 * 
	 * @return xml document containing the sensors information
	 */
	@WebMethod
	public byte[] getSensors() {
		logger.info("Get all the sensors");

		File file = clientProcessor.getSensors();

		byte[] data = null;
		try {
			data = Files.readAllBytes(file.toPath());
		} catch (IOException e) {
			logger.error("Error reading xml file");
			e.printStackTrace();
			return null;
		}
		return data;

	}

	/**
	 * \brief get the sensor datafield
	 * 
	 * Get the datafield from the sensor (selected by id) from the \link
	 * clientProcessor \endlink
	 * 
	 * @param sensorId
	 * @return xml document containing the sensor datafields
	 */
	@WebMethod
	public byte[] getSensorDataFields(int sensorId) {
		logger.info("Get sensor {" + sensorId + "} data fields");

		File file = clientProcessor.getSensorDataFields(sensorId);

		byte[] data = null;
		try {
			data = Files.readAllBytes(file.toPath());
		} catch (IOException e) {
			logger.error("Error reading xml file");
			e.printStackTrace();
			return null;
		}
		return data;
	}

	/**
	 * \brief get sensor data.
	 * 
	 * Get the known data from the selected sensor.
	 * 
	 * @param sensorId
	 * @return xml document containing the sensor data
	 */
	@WebMethod
	public byte[] getSensorData(int sensorId) {
		logger.info("Get all the sensor data");

		File file = clientProcessor.getSensorData(sensorId);
		byte[] data = null;
		try {
			data = Files.readAllBytes(file.toPath());
		} catch (IOException e) {
			logger.error("Error reading xml file");
			e.printStackTrace();
			return null;
		}
		return data;
	}

	/**
	 * \brief get sensor data.
	 * 
	 * Get the known data from the selected sensor. Limited by the minimum and
	 * maximum range.
	 * 
	 * @param sensorId
	 * @param minimum
	 * @param maximum
	 * @return xml document containing the sensor data
	 */
	@WebMethod
	public byte[] getSpecificSensorData(int sensorId, long minimum, long maximum) {
		logger.info("Get specific sensor data");

		File file = clientProcessor.getSensorData(sensorId, minimum, maximum);
		byte[] data = null;
		try {
			data = Files.readAllBytes(file.toPath());
		} catch (IOException e) {
			logger.error("Error reading xml file");
			e.printStackTrace();
			return null;
		}
		return data;
	}

}
